layui.use(['layer','form','table','element'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element;

    var column;
    column = [[
        {field:'title', title: '标题'}
        ,{field:'showCreateTime',width:110,title: '创建时间'}
        ,{field:'',width:520,title: '操作',toolbar: '#barDemo2'}
    ]];

    //初始化数据表格
     table.render({
         elem:"#data"
         ,height: 'full-150'
         ,url:"/admin/project/getTemProjectList"
         ,cellMinWidth: 80
         ,cols: column
         ,page:true
     });

     //监听工具条
     table.on('tool(dataTable)',function (obj) {
         var data = obj.data;
         if(obj.event == 'publish'){
             var title = data.title;

             layer.confirm('确定发布？', function(index){
                 layer.msg('发布中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

                 $.ajax({
                     type:"POST",
                     data:{
                         "title":title
                     }
                     ,url:"/admin/project/publish"
                     ,success:function (result) {
                         layer.confirm(result.message, function(index){
                             layer.close(index);
                         });
                         table.reload("data");
                         form.render();
                     }
                     ,error:function () {
                         layer.msg("系统错误");
                     }

                 });

             });
         }else {
             //编辑项目
            layer.msg("操作错误！");
         }
     });

    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }
     
     //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                title:$("#title").val(),
                typeId:$("#type").val(),
                labelId:$("#label").val(),
                commented:$("#commented").val()
            }
        })
    });;

    $("#add").on("click",function () {

        parent.addTab($(this));

        // parent.layui.element.tabAdd('tabs',{
        //     title:"免费项目新增",
        //     content:"<iframe  src='/admin/project/freeToAdd'  frameborder='0'  style='width:100%;height: 100%'></iframe>",
        //     id:"free_project_add"
        // });
        // rightMenu();
        // iframeWH();
        // parent.layui.element.tabChange('tabs','free_project_add');


    });


    $("#updateSiteMap").on("click",function () {

        $.ajax({
            type:"POST"
            ,url:"/admin/system/updateSiteMap"
            ,success:function (result) {
                layer.msg(result.message);
            }
            ,error:function () {
                layer.msg("系统错误");
            }

        });
    });

});

function toAdd() {
    window.open("/admin/project/freeToAdd", "_blank");
}